<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon.ico">
  <link rel="mask-icon" href="/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.loli.net/css?family=Lato:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/blue/pace-theme-bounce.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"www.kaaaaai.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="抽空把项目里的顶部弹出框封装了一下。">
<meta property="og:type" content="article">
<meta property="og:title" content="Swift - 顶部弹出框封装">
<meta property="og:url" content="https://www.kaaaaai.cn/swift-toppop-upboxencapsulation.html">
<meta property="og:site_name" content="壹拾肆">
<meta property="og:description" content="抽空把项目里的顶部弹出框封装了一下。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2020/08/04/9R7WuCT2b5ezKoN.gif">
<meta property="og:image" content="https://i.loli.net/2020/08/04/9R7WuCT2b5ezKoN.gif">
<meta property="article:published_time" content="2020-08-04T05:24:52.000Z">
<meta property="article:modified_time" content="2022-04-26T01:51:43.360Z">
<meta property="article:author" content="Kaaaaai">
<meta property="article:tag" content="EXP">
<meta property="article:tag" content="iOS">
<meta property="article:tag" content="Swift">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2020/08/04/9R7WuCT2b5ezKoN.gif">


<link rel="canonical" href="https://www.kaaaaai.cn/swift-toppop-upboxencapsulation.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://www.kaaaaai.cn/swift-toppop-upboxencapsulation.html","path":"/swift-toppop-upboxencapsulation.html","title":"Swift - 顶部弹出框封装"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Swift - 顶部弹出框封装 | 壹拾肆</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-102185873-1"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-102185873-1","only_pageview":false}</script>
  <script src="/js/third-party/analytics/google-analytics.js"></script>

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?9578d14210d67becef48447e4f6b3f20"></script>



<link rel="dns-prefetch" href="https://waline-server-kaaaaai.vercel.app"> <script type="text/javascript" src="/js/link-highlight.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="壹拾肆" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">壹拾肆</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">有些安排是多余的 这片空旷对我有益</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-moments"><a href="/bb/" rel="section"><i class="fa fa-coffee fa-fw"></i>碎片</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81"><span class="nav-number">1.</span> <span class="nav-text">代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E4%BA%9B%E6%80%9D%E8%80%83"><span class="nav-number">2.</span> <span class="nav-text">一些思考</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Kaaaaai"
      src="https://s2.loli.net/2022/03/29/GSJKrCRi4owa9hq.png">
  <p class="site-author-name" itemprop="name">Kaaaaai</p>
  <div class="site-description" itemprop="description">🐨🐨</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">66</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="sidebar-button site-overview-item animated">
    <button><i class="fa fa-comment"></i>
      Chat
    </button>
  </div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/Kaaaaai" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Kaaaaai" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/users/8342030/kaaaaai" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;users&#x2F;8342030&#x2F;kaaaaai" rel="noopener" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.instagram.com/edisonlllllk" title="Instagram → https:&#x2F;&#x2F;www.instagram.com&#x2F;edisonlllllk" rel="noopener" target="_blank"><i class="fab fa-instagram fa-fw"></i>Instagram</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:root@kaaaaai.cn" title="E-Mail → mailto:root@kaaaaai.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </div>
      </div>

      
    <!-- 网易云歌曲 -->
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=421203025&auto=1&height=66"></iframe>
        <div class="back-to-top animated" role="button" aria-label="返回顶部">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://www.kaaaaai.cn/swift-toppop-upboxencapsulation.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s2.loli.net/2022/03/29/GSJKrCRi4owa9hq.png">
      <meta itemprop="name" content="Kaaaaai">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="壹拾肆">
      <meta itemprop="description" content="🐨🐨">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Swift - 顶部弹出框封装 | 壹拾肆">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Swift - 顶部弹出框封装
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-08-04 13:24:52" itemprop="dateCreated datePublished" datetime="2020-08-04T13:24:52+08:00">2020-08-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">笔记</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">Waline：</span>
  
    <a title="waline" href="/swift-toppop-upboxencapsulation.html#waline" itemprop="discussionUrl">
      <span class="post-comments-count waline-comment-count" data-path="/swift-toppop-upboxencapsulation.html" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>5 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>抽空把项目里的顶部弹出框封装了一下。</p>
<span id="more"></span>
<p>效果大概是这个样子：<br><img data-src="/images/loading-style-3.gif" data-original="https://i.loli.net/2020/08/04/9R7WuCT2b5ezKoN.gif"></p>
<!--<img align="center" data-src="/images/loading-style-3.gif" data-original="https://i.loli.net/2020/08/04/9R7WuCT2b5ezKoN.gif" width=375px height=677px/>-->

<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><p><strong>自定义弹出框：</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//  KKHeadMessageView.swift</span></span><br><span class="line"><span class="comment">//  VoiceAssistant</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//  Created by Kai Lv on 2020/7/29.</span></span><br><span class="line"><span class="comment">//  Copyright © 2020 Kaaaaai. All rights reserved.</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> UIKit</span><br><span class="line"></span><br><span class="line"><span class="keyword">@objc</span> <span class="keyword">public</span> <span class="keyword">enum</span> <span class="title class_">MessageStyle</span>: <span class="title class_ inherited__">Int</span>&#123;</span><br><span class="line">    <span class="keyword">case</span> none</span><br><span class="line">    <span class="keyword">case</span> success</span><br><span class="line">    <span class="keyword">case</span> error</span><br><span class="line">    <span class="keyword">case</span> warning</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">@objc</span> <span class="keyword">extension</span> <span class="title class_">KKHeadMessageView</span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">var</span> mv: <span class="type">KKHeadMessageView</span>?</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">@objc</span> <span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">func</span> showMessageView(<span class="keyword">_</span> message: <span class="type">String</span>) -&gt; ()&#123;</span><br><span class="line">        <span class="keyword">if</span> mv <span class="operator">!=</span> <span class="literal">nil</span> &#123;</span><br><span class="line">            mv<span class="operator">?</span>.removeFromSuperview()</span><br><span class="line">            mv <span class="operator">=</span> <span class="literal">nil</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        mv <span class="operator">=</span> <span class="type">KKHeadMessageView</span>.<span class="keyword">init</span>(message: message, style: .warning)</span><br><span class="line">        mv<span class="operator">!</span>.show()</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">@objc</span> <span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">func</span> showMessageView(<span class="keyword">_</span> message: <span class="type">String</span>, style:<span class="type">MessageStyle</span>) -&gt; ()&#123;</span><br><span class="line">        <span class="keyword">if</span> mv <span class="operator">!=</span> <span class="literal">nil</span> &#123;</span><br><span class="line">            mv<span class="operator">?</span>.removeFromSuperview()</span><br><span class="line">            mv <span class="operator">=</span> <span class="literal">nil</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        mv <span class="operator">=</span> <span class="type">KKHeadMessageView</span>.<span class="keyword">init</span>(message: message, style: style)</span><br><span class="line">        mv<span class="operator">!</span>.show()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@objc</span> <span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">KKHeadMessageView</span>: <span class="title class_ inherited__">UIView</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">let</span> message: <span class="type">String</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> label_mes <span class="operator">=</span> <span class="type">UILabel</span>.<span class="keyword">init</span>()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> style: <span class="type">MessageStyle</span> <span class="operator">=</span> .none</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">let</span> successBackgroundColor: <span class="type">UIColor</span> <span class="operator">=</span> <span class="type">UIColor</span>(red: <span class="number">86.0</span><span class="regexp">/255, green: 188/</span><span class="number">255</span>, blue: <span class="number">138.0</span><span class="operator">/</span><span class="number">255</span>,  alpha: <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">let</span> warningBackgroundColor: <span class="type">UIColor</span> <span class="operator">=</span> <span class="type">UIColor</span>(red: <span class="number">242.0</span><span class="regexp">/255, green: 153.0/</span><span class="number">255</span>, blue: <span class="number">46.0</span><span class="operator">/</span><span class="number">255</span>,  alpha: <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">let</span> errorBackgroundColor: <span class="type">UIColor</span> <span class="operator">=</span> <span class="type">UIColor</span>(red: <span class="number">192.0</span><span class="regexp">/255, green: 36.0/</span><span class="number">255</span>, blue: <span class="number">37.0</span><span class="operator">/</span><span class="number">255</span>,  alpha: <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">let</span> noneBackgroundColor: <span class="type">UIColor</span> <span class="operator">=</span>  <span class="type">UIColor</span>(red: <span class="number">44.0</span><span class="regexp">/255,  green: 187.0/</span><span class="number">255</span>, blue: <span class="number">255.0</span><span class="operator">/</span><span class="number">255</span>, alpha: <span class="number">1</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">init</span>(<span class="params">message</span>: <span class="type">String</span>, <span class="params">style</span>: <span class="type">MessageStyle</span>)&#123;</span><br><span class="line">        <span class="keyword">self</span>.message <span class="operator">=</span> message</span><br><span class="line">        <span class="keyword">self</span>.style <span class="operator">=</span> style</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">super</span>.<span class="keyword">init</span>(frame: <span class="type">CGRect</span>(x: <span class="number">0</span>, y: <span class="operator">-</span><span class="number">49</span>, width: <span class="type">UIScreen</span>.main.bounds.size.width, height: <span class="number">48</span>))</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">switch</span> style &#123;</span><br><span class="line">        <span class="keyword">case</span> .success:</span><br><span class="line">            <span class="keyword">self</span>.backgroundColor <span class="operator">=</span> <span class="type">KKHeadMessageView</span>.successBackgroundColor</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">case</span> .warning:</span><br><span class="line">            <span class="keyword">self</span>.backgroundColor <span class="operator">=</span> <span class="type">KKHeadMessageView</span>.warningBackgroundColor</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">case</span> .error:</span><br><span class="line">            <span class="keyword">self</span>.backgroundColor <span class="operator">=</span> <span class="type">KKHeadMessageView</span>.errorBackgroundColor</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="keyword">case</span> .none:</span><br><span class="line">            <span class="keyword">self</span>.backgroundColor <span class="operator">=</span> <span class="type">KKHeadMessageView</span>.noneBackgroundColor</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> attributes <span class="operator">=</span> [<span class="type">NSAttributedString</span>.<span class="type">Key</span>.font : <span class="type">UIFont</span>.<span class="keyword">init</span>(name: <span class="string">&quot;PingFangSC-Medium&quot;</span>, size: <span class="number">14</span>)]</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> textSize <span class="operator">=</span> <span class="keyword">self</span>.message.boundingRect(with: <span class="type">CGSize</span>(width: <span class="type">UIScreen</span>.main.bounds.size.width, height: <span class="type">CGFloat</span>.greatestFiniteMagnitude), options: <span class="type">NSStringDrawingOptions</span>.usesLineFragmentOrigin, attributes: attributes <span class="keyword">as</span> [<span class="type">NSAttributedString</span>.Key : <span class="keyword">Any</span>], context: <span class="literal">nil</span>).size</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">let</span> label_mes <span class="operator">=</span> <span class="type">UILabel</span>.<span class="keyword">init</span>(frame: <span class="type">CGRect</span>(x: <span class="number">10</span>, y: <span class="keyword">self</span>.frame.height <span class="operator">-</span> textSize.height <span class="operator">-</span> <span class="number">5</span>, width: textSize.width, height: textSize.height))</span><br><span class="line">        label_mes.font <span class="operator">=</span> <span class="type">UIFont</span>.<span class="keyword">init</span>(name: <span class="string">&quot;PingFangSC-Medium&quot;</span>, size: <span class="number">14</span>)</span><br><span class="line">        label_mes.textColor <span class="operator">=</span> .white</span><br><span class="line">        label_mes.text <span class="operator">=</span> message</span><br><span class="line">        <span class="keyword">self</span>.label_mes <span class="operator">=</span> label_mes</span><br><span class="line">        <span class="keyword">self</span>.addSubview(<span class="keyword">self</span>.label_mes)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">func</span> <span class="title function_">show</span>()&#123;</span><br><span class="line">        <span class="type">UIApplication</span>.shared.keyWindow<span class="operator">?</span>.addSubview(<span class="keyword">self</span>)</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">let</span> animations : () -&gt; () <span class="operator">=</span> &#123;</span><br><span class="line">            <span class="keyword">self</span>.frame.origin.y <span class="operator">=</span> <span class="operator">-</span><span class="number">4</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">let</span> completionAnimations : (<span class="type">Bool</span>) -&gt; () <span class="operator">=</span> &#123; finished <span class="keyword">in</span></span><br><span class="line">            <span class="keyword">if</span> finished &#123;</span><br><span class="line">                <span class="type">UIView</span>.animate(withDuration: <span class="number">5</span>, delay: <span class="number">0</span>, usingSpringWithDamping: <span class="number">1</span>, initialSpringVelocity: <span class="number">5</span>, options: [.curveEaseInOut], animations: &#123;</span><br><span class="line">                    <span class="keyword">self</span>.frame.origin.y <span class="operator">=</span> <span class="operator">-</span><span class="number">49</span></span><br><span class="line">                &#125;)&#123;finished <span class="keyword">in</span></span><br><span class="line">                    <span class="keyword">self</span>.removeFromSuperview()</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="type">UIView</span>.animate(withDuration: <span class="number">1</span>, delay: <span class="number">0</span>, usingSpringWithDamping: <span class="number">10</span>, initialSpringVelocity: <span class="number">10</span>, options: [.curveEaseInOut], animations: animations, completion: completionAnimations)</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     NSCoding not supported. Use init(text, preferences, delegate) instead!</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">required</span> <span class="keyword">public</span> <span class="keyword">init?</span>(<span class="params">coder</span> <span class="params">aDecoder</span>: <span class="type">NSCoder</span>) &#123;</span><br><span class="line">        <span class="built_in">fatalError</span>(<span class="string">&quot;NSCoding not supported. Use init(text, preferences, delegate) instead!&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>调用方式：</strong></p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Swift 调用</span></span><br><span class="line">KKHeadMessageView.showMessageView(<span class="string">&quot;😊这是一个测试的弹窗。&quot;</span>, style: .success)</span><br><span class="line"></span><br><span class="line"><span class="comment">//OC 调用</span></span><br><span class="line">[KKHeadMessageView showMessageView:<span class="string">@&quot;😊这是一个测试的弹窗&quot;</span> style:MessageStyleNone];</span><br></pre></td></tr></table></figure>

<h2 id="一些思考"><a href="#一些思考" class="headerlink" title="一些思考"></a>一些思考</h2><p>如上文所见，基本逻辑相对简单，首先编写一个继承自 <code>UIView</code> 的类，重写 <code>init</code> 方法用来自定义这个 <code>view</code>，为了方便调用，用拓展写了两个类方法 <code>showMessageView()</code>，让后通过定义的枚举来展示不同风格的提示框。虽然作为一个自定义 <code>View</code>，上面的代码可能不是最优写法，但勉强算最适合我当前项目的写法。</p>
<p>也考虑到这个自定义 <code>View</code>，并不对多数人适用。所以想分享一下我在封装这个 <code>View</code> 时所做的一些思考，主要有两点：</p>
<ul>
<li>1.如何写好一个自定义 View？</li>
<li>2.我们应该如何设定自定义 View 的调用方式？</li>
</ul>
<p>那么关于这两个问题的答案从哪里可以找到呢？其实总的来说也就是老生常谈的三个方向：</p>
<ol>
<li>官方文档。</li>
<li>阅读源码。</li>
<li>技术博客。</li>
</ol>
<p>展开来说，自定义 <code>View</code> 的官方文档，其实就是去看看 <code>UIButton</code>、<code>UIView</code> 官方是怎么写的。但因为苹果是不开源的，看不到——虽然网上也有反推的实现，但毕竟有作者个人风格，而且有些还是用 <code>C++</code> 实现的，要模仿着写比较费劲。所以这条先搁置。</p>
<p>然后退而求其次，阅读 GitHub 上 Star 数量较多的自定义视图开源库，学学他们的写法。这里推荐两个库：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://github.com/SwiftKickMobile/SwiftMessages">SwiftMessages</a></li>
<li><a target="_blank" rel="noopener" href="https://github.com/jdg/MBProgressHUD">MBProgressHUD</a></li>
</ul>
<p>在封装消息提示框的时候我也考虑过对外开放的接口做成 <code>MBProgressHUD</code> 中调用方法返回一个单例，或者是和 <code>SwiftMessages</code> 将视图定义和使用分开。但最终因为图省事就选了一个简单的方式。另外还想到一个把 <code>MessageView</code> 用枚举来定义，然后通过实现关联值方法来直接显示，如果换这种实现的话，上面的调用方式可能就会变成这样：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">KKHeadMessageView</span>.success.showMessageView(<span class="string">&quot;😊这是一个测试的弹窗。&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>看起来有点像工厂模式的产物😊。</p>
<p>最后是技术博客。在 Google 上搜，一大推的教你怎么自定义 <code>View</code>，这里推荐两篇我觉得比较好的，有概念讲解，有源码示范，值得参考：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://juejin.im/post/6844903686758481934">如何写好一个自定义View</a></li>
<li><a target="_blank" rel="noopener" href="https://juejin.im/post/6844903878111002637">自定义View都写不好还做什么iOS开发</a></li>
</ul>
<p>最后还有几个小疑惑备注在这里：</p>
<ol>
<li>在自定义 <code>View</code> 中，我们通常在 <code>setSubUI</code> 方法里面设置子视图，在里面写一大堆类似于这种代码：<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> label_mes <span class="operator">=</span> <span class="type">UILabel</span>.<span class="keyword">init</span>(frame: <span class="type">CGRect</span>(x: <span class="number">10</span>, y: <span class="keyword">self</span>.frame.height <span class="operator">-</span> textSize.height <span class="operator">-</span> <span class="number">5</span>, width: textSize.width, height: textSize.height))</span><br><span class="line">        label_mes.font <span class="operator">=</span> <span class="type">UIFont</span>.<span class="keyword">init</span>(name: <span class="string">&quot;PingFangSC-Medium&quot;</span>, size: <span class="number">14</span>)</span><br><span class="line">label_mes.textColor <span class="operator">=</span> .white</span><br><span class="line">label_mes.text <span class="operator">=</span> message</span><br><span class="line"><span class="keyword">self</span>.label_mes <span class="operator">=</span> label_mes</span><br><span class="line"><span class="keyword">self</span>.addSubview(<span class="keyword">self</span>.label_mes)</span><br></pre></td></tr></table></figure>
 在 <a target="_blank" rel="noopener" href="https://casatwy.com/iosying-yong-jia-gou-tan-viewceng-de-zu-zhi-he-diao-yong-fang-an.html">iOS应用架构谈 view层的组织和调用方案</a> 中看到说建议把这种初始化都丢进 <code>getter</code> 里面，而且 <strong>getter 和 setter 全部都放在最后</strong>。另外配合 <code>Swift</code> 中的 <code>extension</code> 可以让代码布局更好看，但也看到唐巧的<a target="_blank" rel="noopener" href="http://blog.devtang.com/2015/03/15/ios-dev-controversy-1/#%E7%9C%81%E5%BF%83%E7%9C%81%E4%BA%8B">iOS 开发中的争议（一）</a>中说：<blockquote>
<p>在类中完全使用 _property 的方式来访问私有成员变量，是不会有内存管理上的问题的。但是使用 self.property 的方式来访问私有变量是不是也是一样不会有内存管理上的问题呢？确实也是，但是有一点需要注意：我们最好不要在 init 和 dealloc 中使用 self.property 的方式来访问成员变量。</p>
</blockquote>
</li>
</ol>
<p>  那么 <code>setSubUI</code> 的写法最优解是什么呢？</p>
<ol start="2">
<li>一个自定义 <code>View</code> 的展现形式通常有：<br>  1）外部初始化后，外部手动添加到视图，比如一些嵌入视图<br>  2）外部初始化后，自动添加到 <code>UIWindow</code> 或者当前显示 <code>Controller</code>，比如一些弹出框视图。<br>  3）也看到一自定义 View 会定义这种初始化方法<code>initWith:(NSString*)Message ToRootView:(UIView*)View</code>。外部传入父视图，然后自己在内部添加自己。<br>  以上三种方式，除了根据实际需求，就把自己添加到父视图上，还有什么更好的方式吗？</li>
</ol>
<p>以上。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    请喝咖啡
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/loading-style-3.gif" data-original="https://s2.loli.net/2022/07/02/Gce8a5QAqrZ3X9R.gif" alt="Kaaaaai 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Kaaaaai
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://www.kaaaaai.cn/swift-toppop-upboxencapsulation.html" title="Swift - 顶部弹出框封装">https://www.kaaaaai.cn/swift-toppop-upboxencapsulation.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/EXP/" rel="tag"><i class="fa fa-tag"></i> EXP</a>
              <a href="/tags/iOS/" rel="tag"><i class="fa fa-tag"></i> iOS</a>
              <a href="/tags/Swift/" rel="tag"><i class="fa fa-tag"></i> Swift</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/a-person-abandoned-by-the-light.html" rel="prev" title="被光放弃的人">
                  <i class="fa fa-chevron-left"></i> 被光放弃的人
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/ios-some-question-about-bluetooth.html" rel="next" title="iOS - 一些蓝牙问题的解决">
                  iOS - 一些蓝牙问题的解决 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="waline"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kaaaaai</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">234k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">3:33</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <!-- time -->
  <div id="show-time">
    <span id="span_dt_dt"></span>
  </div>
  <script>
      function show_date_time() {
        window.setTimeout("show_date_time()", 1000);
        BirthDay = new Date("6/23/2017 19:37:01");
        today = new Date();
        //总时间
        timeold = (today.getTime() - BirthDay.getTime());
        sectimeold = timeold / 1000
        secondsold = Math.floor(sectimeold);
        msPerDay = 24 * 60 * 60 * 1000
        e_daysold = timeold / msPerDay
        daysold = Math.floor(e_daysold);
        e_hrsold = (e_daysold - daysold) * 24;
        hrsold = Math.floor(e_hrsold);
        e_minsold = (e_hrsold - hrsold) * 60;
        minsold = Math.floor((e_hrsold - hrsold) * 60);
        seconds = Math.floor((e_minsold - minsold) * 60);
        span_dt_dt.innerHTML = "已存活 " + daysold + " 天 " + hrsold + " 小时 " + minsold + " 分 " + seconds + " 秒";
    }
    show_date_time();
  </script>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>
<script class="next-config" data-name="chatra" type="application/json">{"enable":true,"async":true,"id":"QXLRxXcsfrRJR2KTG"}</script>
<script src="/js/third-party/chat/chatra.js"></script>
<script async src="https://call.chatra.io/chatra.js"></script>



  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<script class="next-config" data-name="waline" type="application/json">{"lang":"zh-cn","enable":true,"serverURL":"https://waline-server-kaaaaai.vercel.app","cssUrl":"https://unpkg.com/@waline/client@v2/dist/waline.css","commentCount":true,"pageview":false,"placeholder":"请文明评论呀","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"visitor":false,"comment_count":true,"requiredFields":[],"libUrl":"//unpkg.com/@waline/client@v2/dist/waline.js","el":"#waline","comment":true,"path":"/swift-toppop-upboxencapsulation.html"}</script>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v2/dist/waline.css">
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.waline.el).then(() =>
    NexT.utils.getScript(CONFIG.waline.libUrl, { condition: window.Waline })
  ).then(() => 
    Waline.init(Object.assign({}, CONFIG.waline,{ el: document.querySelector(CONFIG.waline.el) }))
  );
});
</script>


        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: true,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var a=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,d=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(t){(a||t)&&(d=o());for(var e,i=0;i<d.length;i++)0<=(e=(e=d[i]).getBoundingClientRect()).bottom&&0<=e.left&&e.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,a,n,o=d[i];e=function(){d=d.filter(function(t){return o!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(o)},(t=o).dataset.loaded||(t.hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(a=new Image,n=t.getAttribute("data-original"),a.onload=function(){t.src=n,t.removeAttribute("data-original"),t.setAttribute("data-loaded",!0),e&&e()},a.onerror=function(){t.removeAttribute("data-original"),t.setAttribute("data-loaded",!1),t.src=n},t.src!==n&&(a.src=n)))}()}function e(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",e),r.addEventListener("resize",e),r.addEventListener("orientationchange",e)}(this);</script></body>
</html>
